<?php
namespace app\admin\model;
use think\Request;
use think\Db;
use think\Model;
class Upload extends Model
{
    //上传电影封面图片
    public function upload($FILES,$upload_type,$upload_path){
        set_time_limit(0);
        ini_set('memory_limit', '30768M');
        $result=array();
        // 获取表单上传文件 例如上传了001.jpg
        $FILES=unserialize($FILES);
        $file = request()->file('file');
        $index=new \app\admin\model\Index();
        // 移动到框架应用根目录/uploads/ 目录下
        if($upload_type=='image'){
            $type_arr = explode('/', $FILES['file']['type']);
            $type = $type_arr[1];
            $size=$FILES['file']['size'];
            if ($type == 'png') {
                header("Content-type: image/png");
            }elseif($type == 'gif'){
                header("Content-type: image/gif");
            } else {
                header("Content-type: image/jpeg");
            }
            $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif,jpeg,webp'])->move(dirname($_SERVER['SCRIPT_FILENAME']). $upload_path);
        }else{
            $info = $file->validate(['size'=>524288000,'ext'=>'mp4'])->move(dirname($_SERVER['SCRIPT_FILENAME']). '/uploads/video');//500m
        }
        if($info){
            $path = $upload_path.'/' . $info->getSaveName();
            $path=str_replace('\\', '/',$path);
            if($upload_type=='image'){
                $source = dirname($_SERVER['SCRIPT_FILENAME']) . $path;//原图片名称
                $dst_img = dirname($_SERVER['SCRIPT_FILENAME']) . $path;//压缩后图片的名称
                $percent=0.3;//图片压缩比
                $index->ys_img($source,$dst_img,$type,$percent,$size);
            }
            $path1=$upload_path.'/'.$info->getSaveName();
            $ftp = new \app\admin\model\Ftp(FTP_HOST,FTP_PORT,FTP_USER,FTP_PASS);     // 打开FTP连接
            $re=$ftp->up_file(dirname($_SERVER['SCRIPT_FILENAME']) .$path,$path1);     // 上传文件
            $ftp->close();
            if($re==1){
            //if($re){
                $result['code']=0;
                $result['msg']='上传成功';
                $result['data']['src']=$upload_path.'/'.str_replace('\\','/',$info->getSaveName());
                if($upload_type=='image'){
                    $result['data']['video_time']='';
                }else{
                    $file_name=mb_convert_encoding(dirname($_SERVER['SCRIPT_FILENAME']).$path,'GB2312',mb_detect_encoding(dirname($_SERVER['SCRIPT_FILENAME']).$path));//上传到资源服务器
                    $php_exec=shell_exec('ffprobe -v quiet -print_format json -show_format -show_streams "'.$file_name.'"');
                    $time_long=floor(json_decode($php_exec)->format->duration);
                    $time_long= (new \DateTime('@0')) ->diff(new \DateTime("@$time_long")) ->format('%H:%I:%S');
                    //$time_long='';
                    $result['data']['video_time']=$time_long;
                }
                unset($info);
                @unlink(dirname($_SERVER['SCRIPT_FILENAME']) .$path);
                @rmdir(dirname($_SERVER['SCRIPT_FILENAME']).$upload_path.'/'.date('Ymd',time()).'/');
            }else{
                $result['code']=1;
                $result['msg']=$re;
            }
        }else{
            // 上传失败获取错误信息
            $result['code']=1;
            $result['msg']=$file->getError();
        }
        return json_encode($result);
    }
    public function del_file($log_path,$now_path,$num=0,$type=1){
        if($log_path && $now_path && $type==1){
            //修改成功
            if($log_path!=$now_path){
                /*if (file_exists(dirname($_SERVER['SCRIPT_FILENAME']) . $log_path)) {
                    unlink(dirname($_SERVER['SCRIPT_FILENAME']) . $log_path);
                }*/
                $ftp = new \app\admin\model\Ftp(FTP_HOST,FTP_PORT,FTP_USER,FTP_PASS);     // 打开FTP连接
                $ftp->del_file($log_path);
                if($num>0){
                    $log_path_arr=explode('.',$log_path);
                    $ftp->del_file($log_path_arr[0].'.m3u8');
                    $ftp->del_file($log_path_arr[0].'.mp4');
                    for($i=0;$i<$num;$i++){
                        $new_log_path=$log_path_arr[0].$i.'.ts';
                        $ftp->del_file($new_log_path);
                    }
                }
            }
        }elseif($log_path && $now_path && $type==2){
            //修改失败
            if($log_path!=$now_path) {
                /*if (file_exists(dirname($_SERVER['SCRIPT_FILENAME']) . $now_path)) {
                    unlink(dirname($_SERVER['SCRIPT_FILENAME']) . $now_path);
                }*/
                $ftp = new \app\admin\model\Ftp(FTP_HOST,FTP_PORT,FTP_USER,FTP_PASS);     // 打开FTP连接
                $ftp->del_file($now_path);
                if($num>0){
                    $now_path_arr=explode('.',$now_path);
                    $ftp->del_file($now_path_arr[0].'.m3u8');
                    $ftp->del_file($now_path_arr[0].'.mp4');
                    for($i=0;$i<$num;$i++){
                        $new_now_path=$now_path_arr[0].$i.'.ts';
                        $ftp->del_file($new_now_path);
                    }
                }
            }
        }elseif(!$log_path && $now_path && $type==1){
            //添加失败/删除
            /*if (file_exists(dirname($_SERVER['SCRIPT_FILENAME']) . $now_path)) {
                @unlink(dirname($_SERVER['SCRIPT_FILENAME']) . $now_path);
            }*/
            $ftp = new \app\admin\model\Ftp(FTP_HOST,FTP_PORT,FTP_USER,FTP_PASS);     // 打开FTP连接
            $ftp->del_file($now_path);
            if($num>0){
                $now_path_arr=explode('.',$now_path);
                $ftp->del_file($now_path_arr[0].'.m3u8');
                $ftp->del_file($now_path_arr[0].'.mp4');
                for($i=0;$i<$num;$i++){
                    $new_now_path=$now_path_arr[0].$i.'.ts';
                    $ftp->del_file($new_now_path);
                }
            }
        }
    }
}